<template>
  <div class="home">
    <div class="top-header">
      <h1 class="main-title">欢迎来到仓库管理系统</h1>
    </div>
    <h2 class="page-title">统计与消息</h2>
    <div class="dashboard">
      <!-- 员工数量和用户数量模块 -->
      <div class="stat-box">
        <h2><img src="../images/人数统计.png" alt="人数统计图标" class="icon" /> 员工与用户数量</h2>
        <div class="stats horizontal">
          <div class="stat-item">
            <h3>员工数量</h3>
            <p>{{ employeeCount }}</p>
          </div>
          <div class="stat-item">
            <h3>用户数量</h3>
            <p>{{ userCount }}</p>
          </div>
        </div>
      </div>

      <!-- 最新公告模块 -->
      <div class="stat-box">
        <h2><img src="../images/最新公告.png" alt="最新公告图标" class="icon" /> 最新公告</h2>
        <ul>
          <li v-for="announcement in announcements" :key="announcement.id">
            {{ announcement.title }}
          </li>
        </ul>
      </div>

      <!-- 最新入库数据模块 -->
      <div class="stat-box">
        <h2><img src="../images/最新入库.png" alt="最新入库图标" class="icon" /> 最新入库数据</h2>
        <div class="stats horizontal">
          <div class="stat-item" v-for="record in incomingRecords" :key="record.id">
            <h3>货物名称：{{ record.goodsName }}</h3>
            <p>入库仓库：{{ record.warehouseName || '未知仓库' }}</p>
            <p>入库数量：{{ record.quantity }} 件</p>
          </div>
        </div>
      </div>

      <!-- 最新出库数据模块 -->
      <div class="stat-box">
        <h2><img src="../images/最新出库.png" alt="最新出库图标" class="icon" /> 最新出库数据</h2>
        <div class="stats horizontal">
          <div class="stat-item" v-for="record in outgoingRecords" :key="record.id">
            <h3>货物名称：{{ record.goodsName }}</h3>
            <p>出库仓库：{{ record.warehouseName || '未知仓库' }}</p>
            <p>出库数量：{{ record.quantity }} 件</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'Home',
  data() {
    return {
      employeeCount: 0, // 员工数量
      userCount: 0, // 用户数量
      announcements: [],
      incomingRecords: [], // 最新入库数据
      outgoingRecords: []  // 最新出库数据
    };
  },
  mounted() {
    this.getEmployeeCountAndUserCount();
    this.getLatestAnnouncements();
    this.getLatestIncomingRecords();
    this.getLatestOutgoingRecords();
  },
  methods: {
    async getEmployeeCountAndUserCount() {
      try {
        const employeeResponse = await axios.get('http://localhost:8080/employee/count');
        this.employeeCount = employeeResponse.data.data || 0;  // 防止数据为空
        const userResponse = await axios.get('http://localhost:8080/user/count');
        this.userCount = userResponse.data.data || 0;  // 防止数据为空
      } catch (error) {
        console.error('Error fetching employee or user count', error);
      }
    },

    async getLatestAnnouncements() {
      try {
        const response = await axios.get('http://localhost:8080/announcement/latest');
        this.announcements = response.data || [];  // 防止数据为空
      } catch (error) {
        console.error('Error fetching latest announcements', error);
      }
    },

    async getLatestIncomingRecords() {
      try {
        const response = await axios.get('http://localhost:8080/record/incoming/latest');
        this.incomingRecords = response.data || [];  // 防止数据为空
      } catch (error) {
        console.error('Error fetching latest incoming records', error);
      }
    },

    async getLatestOutgoingRecords() {
      try {
        const response = await axios.get('http://localhost:8080/record/outgoing/latest');
        this.outgoingRecords = response.data || [];  // 防止数据为空
      } catch (error) {
        console.error('Error fetching latest outgoing records', error);
      }
    }
  }
};
</script>

<style scoped>
.home {
  text-align: center;
  padding: 20px;
}

.top-header {
  margin-bottom: 20px;
}

.main-title {
  font-size: 32px;
}

.page-title {
  font-size: 24px;
  margin-top: 10px;
  text-align: center;
}

.dashboard {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.stat-box, .announcements, .news {
  background: #f2f2f2;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.stat-box h2, .announcements h2, .news h2 {
  margin-top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon {
  width: 30px;
  height: 30px;
  margin-right: 10px;
}

.stats.horizontal {
  display: flex;
  justify-content: space-between;
}

.stats .stat-item {
  text-align: center;
  flex: 1;
}

.stats .stat-item h3 {
  font-size: 16px;
  margin: 5px 0;
}

ul {
  padding-left: 20px;
}
</style>
